import React from 'react';
import router from 'umi/router';
import Link from 'umi/link'

import { connect } from 'dva';

import { Card, Divider, Button, Table,Modal } from 'antd';

import DescriptionList from '@/components/DescriptionList';
const { Description } = DescriptionList;

const listPath = '/AccountStock/Stocks'
const lineFormPath = '/AccountStock/StockLineForm'
const formPath = '/AccountStock/StockForm'

@connect(({ accountStock }) => ({ accountStock }))
class Profile extends React.Component {
  state = {

    visibleDel: false,
    delMessage: '',
    visiblePost: false,
    visibleUnpost: false,

  }

  treeview = [
      {
        title: '',
        dataIndex: '_',
        render: (_, { id }) => {
          return <Link to={`${lineFormPath}?id=${id}`}>编辑</Link>;
        },
      },
      {
        title: '',
        dataIndex: '_1',
        render: (_, { id }) => {
          return <Button onClick={()=>this.delLine(id)}>删除</Button>
        },
      },
      { title: '产品', dataIndex: 'product_id.name', },
      { title: '入库', dataIndex: 'quantity_in', },
      { title: '出库', dataIndex: 'quantity_out', },
      { title: '对方科目', dataIndex: 'opp_account_id.name', },
      { title: '数量', dataIndex: 'quantity', },
      { title: '单价', dataIndex: 'price', },
      { title: '金额', dataIndex: 'amount', },
      { title: '摘要', dataIndex: 'name', },
  ]

  id = 0

  async componentDidMount() {
    const { location: {query: { id } } } = this.props;
    this.id = Number(id)
    if (this.id) {
      const { dispatch } = this.props;
      dispatch({
        type: 'accountStock/view',
        payload:  { id: this.id }
      });
    }
  }


  goto_createForm() {
    const { accountStock: { record } } = this.props;
    if(record.id){
      router.push(`${formPath}?id=${record.id}`);
    }
  }

  showDeleteMove() {
    this.setState({
      visibleDel: true
    })

  }

  showPostMove() {
    this.setState({
      visiblePost: true
    })

  }

  showUnpostMove() {
    this.setState({
      visibleUnpost: true
    })

  }

  handleCancel(){
    this.setState({
      visibleDel: false,
      visiblePost: false,
      visibleUnpost: false,
    })
  }

  async delLine( line_id ){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountStock/write',
        payload:  {
          id: this.id ,
          vals: {line_ids: [[2,line_id,0]]}

        }
      });

    }

  }


  async delMove(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountStock/unlink',
        payload:  { id: this.id }
      });

      const { accountStock } = this.props;
      if( accountStock.id ){
        this.setState({
          delMessage: '不能删除该记录'
        })
      }
      else{
        router.push(listPath);

      }
    }
  }

  async postMove(){

    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountStock/post',
        payload:  { id:this.id }
      });

      this.setState({
        visiblePost: false
      })

    }
  }

  async unpostMove(){
    if (this.id) {
      const { dispatch } = this.props;
      await dispatch({
        type: 'accountStock/unpost',
        payload:  { id:this.id  }
      });

      this.setState({
        visibleUnpost: false
      })

    }
  }


  render() {
    const { accountStock: { record } } = this.props;

    const { visiblePost, visibleUnpost,
      visibleDel,delMessage } = this.state

    return (
      <Card bordered={false}>

        <DescriptionList size="large" title='操作控制' style={{ marginBottom: 32 }}>
          <Description >
            <Button onClick={()=>this.goto_createForm()}>编辑</Button>
          </Description>
          <Description >
            <Button onClick={()=>this.showDeleteMove()}>删除</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.showPostMove()}>过账</Button>
          </Description>

          <Description >
            <Button onClick={()=>this.showUnpostMove()}>反过账</Button>
          </Description>

        </DescriptionList>

        <Modal title='删除'
          visible={visibleDel}
          onOk={()=>this.delMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认删除?
          {delMessage}

        </Modal>

        <Modal title='过账'
          visible={visiblePost}
          onOk={()=>this.postMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认过账?
        </Modal>


        <Modal title='反过账'
          visible={visibleUnpost}
          onOk={()=>this.unpostMove()}
          onCancel={()=>this.handleCancel()}
        >
          确认 反过账?
        </Modal>

        <DescriptionList size="large" title='信息' style={{ marginBottom: 32 }}>
          <Description term='名称' >{record.name}</Description>
          <Description term='参考' >{record.ref}</Description>
          <Description term='状态' >{record.state}</Description>
          <Description term='日期' >{record.date}</Description>
          <Description term='产品类型' >{record.product_type}</Description>
          <Description term='出入库方向' >{record.direction}</Description>
          <Description term='出入库方式' >{record.move_type}</Description>
          <Description term='金额' >{record.amount}</Description>
          <Description term='账簿' >{record.journal_id && record.journal_id.name}</Description>
          <Description term='科目' >{record.account_id && record.account_id.name}</Description>
          <Description term='对方科目' >{record.opp_account_id && record.opp_account_id.name}</Description>
        </DescriptionList>

        <Divider style={{ marginBottom: 32 }} />
        <DescriptionList size="large" title='明细' style={{ marginBottom: 32 }}>
          <Link to={`${lineFormPath}?stock_id=${record.id}`}>新增明细行</Link>
          <Table columns={this.treeview} dataSource={record.line_ids} rowKey="id" />
        </DescriptionList>
      </Card>
    );
  }
}

//

export default Profile;

